package com.wwu.system.controller;

import com.wwu.common.constant.CommConst;
import com.wwu.common.enums.MenuBtnEnum;
import com.wwu.common.web.ResultObj;
import com.wwu.common.web.ResultPage;
import com.wwu.system.common.aspect.Log;
import com.wwu.system.entity.SysQuartz;
import com.wwu.system.service.ISysQuartzService;
import com.wwu.system.vo.QueryVO;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 定时任务前端控制器
 * @author 一蓑烟雨
 * @date 2023/5/31 9:37
 */
@Api(tags = "定时任务控制器(SysQuartzController)")
@ApiSort(3)
@Controller
@RequestMapping("/system/sysQuartz")
public class SysQuartzController {
    @Resource
    private ISysQuartzService sysQuartzService;

    /**
     * 请求转发到定时任务主页面
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/7/11 14:54
     */
    @GetMapping("/quartzMain")
    public String quartzMainHtml(Model model){
        return "system/sysquartz/quartz-main";
    }

    /**
     * 请求定时任务新增或更新页面
     * @param quartzId 任务Id
     * @param model
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/8/10 18:18
     */
    @GetMapping("/quartzAddOrUpdate")
    public String quartzAddOrUpdateHtml(String quartzId,Model model){
        if(StringUtils.hasText(quartzId)){
            model.addAttribute(CommConst.SYS_QUARTZ,sysQuartzService.getById(quartzId));
        }
        return "system/sysquartz/quartz-add-edit";
    }

    @Log(menuName = "定时任务",menuBtnEnum = MenuBtnEnum.SHOW)
    @ApiOperation(value = "查询定时任务")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数",paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数",paramType = "query", required = true),
            @ApiImplicitParam(name = "sysQuartz", value = "定时任务实体"),
            @ApiImplicitParam(name = "queryVO", value = "前端查询条件封装实体类"),
    })
    @GetMapping("/quartzList")
    @ResponseBody
    public ResultPage<SysQuartz> quartzList(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "5000") Integer limit,
                                            SysQuartz sysQuartz, QueryVO queryVO){
        return sysQuartzService.getQuartzListPage(page,limit,sysQuartz,queryVO);
    }


    @Log(menuName = "定时任务",menuBtnEnum = MenuBtnEnum.ADD)
    @ApiOperation(value = "添加定时任务")
    @ApiOperationSupport(order = 2)
    @PostMapping("/saveQuartz")
    @ResponseBody
    public  ResultObj saveQuartz(@RequestBody SysQuartz sysQuartz){
        sysQuartzService.saveQuartz(sysQuartz);
        return ResultObj.success("定时任务添加成功");
    }

    @Log(menuName = "定时任务",menuBtnEnum = MenuBtnEnum.EDIT)
    @ApiOperation(value = "更新定时任务")
    @ApiOperationSupport(order = 3)
    @PutMapping("/updateQuartz")
    @ResponseBody
    public ResultObj updateQuartz(@RequestBody SysQuartz sysQuartz){
        sysQuartzService.updateQuartz(sysQuartz);
        return ResultObj.success("定时任务更新成功");
    }

    @Log(menuName = "定时任务",menuBtnEnum = MenuBtnEnum.STATUS)
    @ApiOperation(value = "更新定时任务状态")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "quartzId", value = "定时任务Id", required = true),
            @ApiImplicitParam(name = "quartzStatus", value = "定时任务状态(0:启动;1:停止)",required = true)
    })
    @PutMapping("/updateQuartzStatus")
    @ResponseBody
    public ResultObj updateQuartzStatus(String quartzId,Integer quartzStatus){
        sysQuartzService.updateQuartzStatus(quartzId,quartzStatus);
        return ResultObj.success("定时任务状态更新成功");
    }

    @Log(menuName = "定时任务",menuBtnEnum = MenuBtnEnum.DEL)
    @ApiOperation(value = "删除定时任务")
    @ApiOperationSupport(order = 5)
    @DeleteMapping("/deleteQuartz/{quartzIds}")
    @ResponseBody
    public ResultObj deleteQuartz(@ApiParam(name = "quartzIds", value = "定时任务id集合，以逗号分隔", required = true)
                                @PathVariable String quartzIds){
        sysQuartzService.deleteQuartz(Arrays.asList(quartzIds.split(CommConst.SPLIT_COMMA)));
        return ResultObj.success("定时任务删除成功");
    }
}